package com.cheer.dto;

import com.cheer.entity.Applicant;
import com.cheer.enums.TitleEnum;
import com.cheer.vo.OrganizationPageVO;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Data
@Accessors(chain = true)
public class OrganizationPageDTO {

    private Long orgId;

    private String orgName;

    private String parentOrgName;

    private Long parentOrgId;

    private Boolean secondAdmin;

    private String orgPath;

    public OrganizationPageVO build(List<Applicant> applicants) {
        OrganizationPageVO organizationPageVO = new OrganizationPageVO();
        organizationPageVO.setOrgName(orgName);
        if (parentOrgId != 0 && secondAdmin) {
            organizationPageVO.setParentOrgName("团省委");
        } else {
            organizationPageVO.setParentOrgName(parentOrgName);
        }
        List<Applicant> orgApplicants = applicants.stream()
                .filter(applicant -> StringUtils.startsWith(applicant.getOrgPath(), orgPath))
                .collect(Collectors.toList());
        organizationPageVO.setApply(orgApplicants.size());
        organizationPageVO.setCity((int) orgApplicants.stream()
                .filter(applicant -> applicant.getTitle() == TitleEnum.CITY.getType()).count());
        organizationPageVO.setProvince((int) orgApplicants.stream()
                .filter(applicant -> applicant.getTitle() == TitleEnum.PROVINCE.getType()).count());
        organizationPageVO.setNation((int) orgApplicants.stream()
                .filter(applicant -> applicant.getTitle() == TitleEnum.NATION.getType()).count());
        return organizationPageVO;
    }

}
